package com.teziyou.commodity.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import jodd.bean.BeanUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mytravel.platform.bean.DictionaryBean;
import com.teziyou.commodity.logic.CategoryLogic;
import com.teziyou.commodity.service.DictService;
import com.teziyou.commodity.util.Lang;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/category")
@Controller
public class CategoryController extends BaseController {
	
	@Autowired
	private DictService dictService;
	@Autowired
	private CategoryLogic categoryLogic;
	
	@RequestMapping("/index")
	public String index(Map map) {
		//map.put("trees", JSON.toJSONString(categoryLogic.getCategoryTree()));
		return "category/index";
	}
	
	@RequestMapping("/load")
	public String load(int id, Map map) {
		DictionaryBean dict = dictService.getDictionaryById(id);
		map.put("dict", dict);
		return "category/form";
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public String save(HttpServletRequest req) {
		Map param = super.getParaMap(req);
		param.put("orderNO", Lang.toInt(param.get("orderNO")));
		DictionaryBean dict = new DictionaryBean();
		BeanUtil.populateBean(dict, param);
		try {
			categoryLogic.saveCategory(dict);
			System.err.println(dict);
			return success("保存成功！！");//, categoryLogic.getCategoryTree());
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@ResponseBody
	@RequestMapping("/del")
	public String delete(int id) {
		try {
			dictService.deleteDictionaryTrift(id);
			return success("删除成功！！");//, categoryLogic.getCategoryTree());
		} catch (Exception e) {
			e.printStackTrace();
			return fail("删除失败！！");
		}
		
	}

}
